<template>
  <div class="resumeItemContainer">
    <div class="resumeItemTitle">
      <div class="resumeItemTitleFlag"></div>
      <span class="resumeItemTitleContent">工作经历</span>
      <span class="resumeItemTitleAddIcon" @click="handleAddClick"><img :src="resumeItemAddIcon"
          class="resumeItemAddIcon"> 添加</span>
    </div>
    <div class="resumeItemMain">
      <div class="resumeItemDisplayList">
        <div class="resumeItemDisplayItem" v-for="workItem,index in resumeDataList" :key="workItem.workId">
          <div class="resumeItemDisplayItemEditButtons">
            <template v-if="!workItem.editStatus">
              <span class="resumeItemDisplayItemEditButton" @click="changeEditStatus(workItem,true)">
                <img :src="myResumeEditIcon" /> 编辑</span>
              <span class="resumeItemDisplayItemEditButton" @click="handleDeletClick(workItem)"
                v-if="resumeDataList.length>1">
                <img :src="myResumeDeleteIcon" />删除</span>
            </template>
          </div>
          <div class="displayBlock" v-if="!workItem.editStatus">
            <!-- 非编辑状态 -->
            <div class="displayInfo">
              <span class="bold"> {{workItem.company}} </span>
              <span class="divide" v-if="workItem.jtzw"></span>
              <span class="bold"> {{workItem.jtzw}}</span>
              <span class="divide" v-if="workItem.datev2"></span>
              <span> {{workItem.datev2}}</span>
            </div>
            <div class="summaryContainer">
              <div class="summary richContent" :ref="`richContent${index}`" v-html="workItem.workdesc"></div>
              <div class="expendContainer" @click="expandContent(workItem,index)" v-if="workItem.showExpend">
                <el-button type="text" :icon="workItem.expendButtonText=='展开'?'el-icon-arrow-down':'el-icon-arrow-up'">
                  {{workItem.expendButtonText||"展开"}}
                </el-button>
              </div>
            </div>
          </div>
          <div class="editBlock" v-else :id="workItem.editStatus?'edittingBlock':''">
            <!-- 编辑状态 -->
            <div class="resumeItemEdit">
              <el-form :ref="'workEditForm'+workItem.workId" :rules="rules" :model="workItem.editForm" inline>
                <el-form-item label="公司名称:" prop="company">
                  <el-input v-model="workItem.editForm.company" placeholder="请输入公司名称" maxlength="20"></el-input>
                </el-form-item>
                <el-form-item label="公司行业:" prop="totalid">
                  <el-select v-model="workItem.editForm.totalid" placeholder="请选择公司行业"
                    @change="(e)=>{handleTotalChange(e,workItem)}">
                    <el-option v-for="item in trade" :key="item.totalid" :label="item.totalname" :value="item.totalid">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="职位类型:" prop="jtzwtype">
                  <el-cascader ref="workInfo_jtzwtype_cascader"
                    :visible="cascaderVisibleStates.workInfo_jtzwtype_cascader" v-model="workItem.editForm.jtzwtype"
                    filterable :options="workItem.positionList"
                    :props="{...cascaderProps,value:'selfName',label:'selfName',children:'children'}" :emitPath="true"
                    @change="(e)=>{handleCascaderChange(e,workItem,'positionInfo')}" :style="{width:'100%'}" clearable
                    placeholder="请先选择公司行业后选择职位类型" />
                </el-form-item>
                <el-form-item label="职位名称" prop="jtzw">
                  <el-input v-model="workItem.editForm.jtzw" placeholder="请输入职位名称" maxlength="20"></el-input>
                </el-form-item>
                <el-form-item label="在职时间" prop="dateArr">
                  <el-date-picker v-model="workItem.editForm.dateArr" size="small" type="monthrange" align="right"
                    unlink-panels value-format="yyyy-MM-dd" range-separator="至" start-placeholder="开始时间"
                    end-placeholder="结束时间">
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="工作经历内容" class="textAreaFormItem" prop="workdesc">
                  <el-input type="textarea" :rows="8" placeholder="请简述工作背景，曾负责的工作内容，克服过的困难，取得的工作成果"
                    v-model="workItem.editForm.workdesc" :maxlength="1600" show-word-limit>
                  </el-input>
                </el-form-item>
                <!-- <el-form-item label="公司名称是否保密">
                  <el-select v-model="workItem.editForm.companykj" placeholder="请选择">
                    <el-option v-for="item in secretList" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="是否为实习经历">
                  <el-select v-model="workItem.editForm.is_practice" placeholder="请选择">
                    <el-option v-for="item in secretList" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item> -->
                <el-form-item class="wholeLineFormItem">
                  <div class="workSecretButtons">
                    <div class="workSecretButtonItem">
                      <el-checkbox label="公司名称是否保密" v-model="workItem.editForm.companykj" true-label="1"
                        false-label="0"></el-checkbox>
                      <el-checkbox label="是否为实习经历" v-model="workItem.editForm.is_practice" true-label="1"
                        false-label="0"></el-checkbox>
                    </div>
                  </div>
                  <div class="submitButtons">
                    <el-button size="small" @click="handleCancelForItem(workItem)">取 消</el-button>
                    <el-button size="small" type="primary" @click="handleSaveForItem(workItem)">完 成</el-button>
                  </div>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
      </div>

    </div>
  </div>
</template>
<script>
import myResumeItemsMixin from "@/mixin/myResumeItemsMixin.js";
import trade from "@/utils/tradeForExp";
import { secretList } from "@/utils/resumeData";
export default {
  name: "workInfo",
  mixins: [myResumeItemsMixin],
  props: {
    resumeData: {
      type: Array,
      default: () => [],
    },
    editStatus: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      trade,
      secretList,
      resumeDataList: [], //
      positionList: [], //接口获取
      rules: {
        company: [
          { required: true, message: "请输入公司名称", trigger: "blur" },
          {
            min: 2,
            max: 50,
            message: "长度在 2 到 50 个字符",
            trigger: "blur",
          },
        ],
        totalid: [
          { required: true, message: "请选择公司行业", trigger: "change" },
        ],
        jtzwtype: [
          { required: true, message: "请选择职位类型", trigger: "change" },
        ],
        jtzw: [
          { required: true, message: "请输入职位名称", trigger: "blur" },
          {
            min: 2,
            max: 50,
            message: "长度在 2 到 50 个字符",
            trigger: "blur",
          },
        ],
        dateArr: [
          {
            type: "array",
            required: true,
            message: "请选择在职时间",
            trigger: "change",
          },
        ],
        workdesc: [
          { required: true, message: "请输入工作经历内容", trigger: "blur" },
          {
            min: 2,
            max: 1600,
            message: "长度在 2 到 1600 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    resumeData: {
      immediate: true,
      handler(newValue, oldValue) {
        if (!newValue) return;
        //console.log("工作经历workInfo基本信息更新：", newValue);
        this.resumeDataList = newValue.map((workItem, index) => {
          if (!workItem.hasOwnProperty("editStatus")) {
            this.$set(workItem, "editStatus", false);
          }
          if (!workItem.hasOwnProperty("editForm")) {
            this.$set(workItem, "editForm", {
              ...workItem,
              dateArr: [
                workItem.startdate,
                workItem.stopdate ? workItem.stopdate : "",
              ],
              totalid: workItem.totalid == 0 ? null : workItem.totalid,
            });
          }
          if (!workItem.hasOwnProperty("showExpend")) {
            this.isOverflowing(workItem, `richContent${index}`);
          }
          return workItem;
        });
      },
    },
    editStatus: {
      handler(newValue, oldValue) {
        //如果新值是false要把所有的editStatus改为false
        if (newValue == false) {
          this.resumeDataList.forEach((item) => {
            item.editStatus = false;
            if (item.isNew) {
              this.resumeDataList.splice(this.resumeDataList.indexOf(item), 1); // 新的删除
            }
          });
        }
      },
    },
  },
  methods: {
    //根据行业获取职位类型
    async reqGetPositionType(workItem) {
      const finalParams = {
        op: "person_intended_industry_busi",
        func: "getTradeZwCache",
        version: "v7.8.0",
        forName: "iphone",
        person_id: this.personId,
        param: {
          tradeid: workItem.editForm.totalid,
        },
      };
      this.resumeGeneralInterface(
        finalParams,
        (res) => {
          this.$set(workItem, "positionList", res.data);
          //console.log("potitionList", workItem);
        },
        () => {}
      );
    },
    //工作经历保存
    async handleSaveForItem(workItem) {
      this.$refs[`workEditForm` + workItem.workId][0].validate((valid) => {
        if (valid) {
          const {
            is_practice,
            company,
            jtzw,
            dateArr,
            salarymonth = null,
            workdesc,
            tradeid = 0,
            jtzwtype,
            companykj,
            totalid,
            workId,
          } = workItem.editForm;
          const [startdate, stopdate] = dateArr;
          const finalParams = {
            op: "cma_weixin_deal",
            func: "updataObjColumn",
            forName: "iphone",
            param: {
              person_id: this.personId,
              conditionArr: {
                work: {
                  is_practice,
                  startdate,
                  company,
                  jtzw,
                  stopdate,
                  salarymonth,
                  workdesc,
                  tradeid,
                  jtzwtype,
                  companykj,
                  totalid,
                  workId,
                },
              },
            },
          };

          //console.log("修改工作经历：", finalParams);
          // return;
          this.resumeGeneralInterface(
            finalParams,
            () => {
              this.changeEditStatus(workItem, false);
              workItem.isNew = false;
            },
            () => {}
          );
        } else {
          //console.log("error submit!!");
          return;
        }
      });
    },
    handleCancelForItem(workItem) {
      this.changeEditStatus(workItem, false);
      if (workItem.isNew) {
        this.resumeDataList.splice(this.resumeDataList.indexOf(workItem), 1);
      } else {
        const {
          is_practice,
          company,
          jtzw,
          dateArr,
          salarymonth = null,
          workdesc,
          tradeid = 0,
          jtzwtype,
          companykj,
          totalid,
          workId,
        } = workItem;
        this.$set(workItem, "editForm", {
          is_practice,
          company,
          jtzw,
          dateArr,
          salarymonth,
          workdesc,
          tradeid,
          jtzwtype,
          companykj,
          totalid,
          workId,
          dateArr: [workItem.startdate, workItem.stopdate],
        });
      }
    },
    //公司行业选择
    handleTotalChange(e, item) {
      this.reqGetPositionType(item);
    },
    handleCascaderChange(value, aimItem, prop) {
      switch (prop) {
        case "positionInfo":
          aimItem.positionList.forEach((item) => {
            if (item.selfId === value) {
              aimItem.editForm.jtzwtype = item.selfId;
              // aimItem.editForm.zwname = item.selfName;
            }
          });
        default:
          break;
      }
    },
    handleDeletClick(workItem) {
      this.$confirm("确定删除该条工作经历吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //下面才是删除方法
          const finalParams = {
            op: "cma_weixin_deal",
            func: "updataObjColumn",
            forName: "iphone",
            param: {
              person_id: this.personId,
              conditionArr: {
                delwork: workItem.workId,
              },
            },
          };
          this.resumeGeneralInterface(
            finalParams,
            () => {},
            () => {}
          );
        })
        .catch(() => {});
    },
    //由于需要发送请求，所以不放入mixin中
    changeEditStatus(workItem, status) {
      //意向职位列表是通过接口获取的，为了复现需要在展示表格的时候就获取
      if (
        status == true &&
        !(workItem.positionList && workItem.positionList.length)
      ) {
        this.reqGetPositionType(workItem);
      }
      if (status) {
        let editingIndex = this.checkEditingItem();
        if (editingIndex != -1) {
          this.$confirm("有内容没有保存，确定退出编辑吗？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              //全部关闭编辑再打开当前的
              this.resumeList.forEach((item) => {
                item.editStatus = false;
              });
              this.resumeDataList.forEach((item) => {
                item.editStatus = false;
              });
              workItem.editStatus = status;
              this.$emit("update:editStatus", status);
            })
            .catch(() => {});
        } else {
          workItem.editStatus = status;
          this.$emit("update:editStatus", status);
        }
      } else {
        //如果是设置为false则直接设置当前的状态
        workItem.editStatus = status;
        this.$emit("update:editStatus", status);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.resumeItemContainer {
  display: flex;
  flex-direction: column;
  margin: 20px 0 16px;
  .resumeItemTitle {
    display: flex;
    align-items: center;
    .resumeItemTitleFlag {
      width: 4px;
      height: 19px;
      background: #0b6eff;
      border-radius: 2px;
      margin-right: 10px;
    }
    .resumeItemTitleContent {
      font-family: PingFangSC, PingFang SC;
      font-weight: 500;
      font-size: 20px;
      color: #333333;
      margin-right: 19px;
    }
    .resumeItemTitleAddIcon {
      display: flex;
      align-items: center;
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      font-size: 14px;
      color: #4880ff;
      cursor: pointer;
      .resumeItemAddIcon {
        width: 20px;
        height: 20px;
        margin-right: 8px;
      }
    }
  }
  .resumeItemMain {
    margin-top: 16px;
    .resumeItemDisplayList {
      .resumeItemDisplayItem {
        position: relative;
        margin-bottom: 16px;
        &:last-of-type {
          margin-bottom: 0px;
        }
        &:first-of-type {
          .displayBlock {
            .summaryContainer {
              .summary {
                margin-top: 0px;
              }
            }
          }
        }
        &:hover {
          .displayBlock {
            background: #f8f8f8;
            border-radius: 8px;
            padding: 16px 16px 16px 16px;
          }
          .resumeItemDisplayItemEditButtons {
            display: block;
          }
        }
        .resumeItemDisplayItemEditButtons {
          display: none;
          position: absolute;
          top: -18px;
          right: 0;
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 15px;
          color: #4880ff;
          line-height: 18px;
          & > span {
            display: inline-flex;
            align-items: center;
            cursor: pointer;
            margin-left: 23px;
            user-select: none;
            & > img {
              width: 16px;
              height: 16px;
              margin-right: 5px;
            }
          }
        }
        .displayBlock {
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 15px;
          color: #666666;
          line-height: 18px;
          text-align: left;
          font-style: normal;
          .displayInfo {
            display: flex;
            align-items: center;
          }
          .summary {
            font-family: PingFangSC, PingFang SC;
            font-weight: 400;
            font-size: 14px;
            color: #888888;
            line-height: 21px;
            text-align: left;
            font-style: normal;
            margin-top: 9px;
            margin-bottom: 0px;
          }
          .bold {
            font-weight: bold;
            font-size: 15px;
            color: #000000;
            line-height: 21px;
          }
          .divide {
            width: 1px;
            height: 11px;
            background: #cccfd7;
            margin: 0 12px;
          }
        }
        .editBlock {
          margin-top: 2px;
          background: #f8f8f8;
          border-radius: 8px;
          padding: 16px;
        }
      }
    }
  }
}
::v-deep .el-form {
  .el-form-item {
    display: inline-flex;
    flex-direction: column;
    min-width: 251px;
    width: calc(33% - 40px);
    margin-right: 24px;
    margin-bottom: 16px;
    margin-top: 0px;
    .el-form-item__content {
      .el-input_inner {
        width: 100%;
      }
      .el-select {
        width: 100%;
      }
      .el-date-editor {
        height: 40px;
        .el-range-input {
          background-color: transparent;
          color: rgb(96, 98, 102);
        }
        .el-range-separator {
          line-height: 32px;
        }
      }
    }
    &.textAreaFormItem {
      width: 100%;
      margin-bottom: 14px;
      .el-textarea {
        textarea {
        }
      }
    }
    &.wholeLineFormItem {
      width: 100%;
      margin-bottom: 0px;
      .el-form-item__content {
        display: flex;
        justify-content: space-between;
        .workSecretButtons {
          margin-top: -10px;
          .el-checkbox {
          }
        }
        .submitButtons {
          display: flex;
          justify-content: flex-end;
          .el-button {
            display: flex;
            justify-content: center;
            align-items: center;
            width: 100px;
            height: 30px;
            background: #ffffff;
            border-radius: 4px;
            border: 1px solid #cdcdcd;
            font-family: PingFang-SC, PingFang-SC;
            font-weight: bold;
            font-size: 14px;
            text-align: left;
            font-style: normal;
            padding: 0;
            &.el-button--primary {
              width: 100px;
              height: 30px;
              background: #4880ff;
              border-radius: 4px;
            }
          }
        }
        &::before {
          display: none;
        }
        &::after {
          display: none;
        }
      }
    }
    .el-form-item__label {
      height: 22px;
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      font-size: 14px;
      color: #666660;
      line-height: 20px;
      text-align: left;
      font-style: normal;
    }
  }
}

::v-deep .el-textarea .el-input__count {
  background-color: transparent;
  bottom: -7px;
}
</style>